<?php
/**
 * 前台用户注册登录系统. 
 */
defined("APP_VERSION") OR die("No Access");
class LoginAction extends CoreAction {
	
	public function initAction() {
		session_start();
		$this->data['links'] = array();
		$this->data['links'][] = 'css/header_menu.css';
		$this->data['links'][] = 'css/base-old.v8.css';
		$this->data['links'][] = 'css/login_v5.css';
		
		//如果用户已经登录,挑战到用户中心.
		if (isset($_SESSION['member']['member_id'])) {
			$this->redirect('member', 'index');
		}
	}
	
	/**
	 * 用户登录界面. 
	 */
	public function index() {
		if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
			if ($_SESSION['validate_code'] != $_POST['validate']) {
				$this->message('back', '验证码填写有误,请重新填写.', '用户登录');
			}
			
			$username = isset($_POST['username']) ? $_POST['username'] : '';
			$password = isset($_POST['password']) ? $_POST['password'] : '';
			
			if (!preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/', $username)) {
				$this->message('back', '用户邮箱不符合要求,请重新填写.', '用户登录');
			}

			$member_model = new MemberModel();
			if (($member = $member_model->login($username, $password))) {
				$_SESSION['member'] = $member;
				$this->redirect('member', 'index');
			} else {
				$this->message('back', '账户或密码错误,请重新登录.', '用户登录');
			}
		}
		
		$this->display();
	}
	
	/**
	 * 用户注册界面. 
	 */
	public function register() {
		if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
			$data = array();
			$data['nickname'] = isset($_POST['nickname']) ? $_POST['nickname'] : '';
			$data['uname'] = isset($_POST['email']) ? $_POST['email'] : '';
			$data['password'] = isset($_POST['password']) ? $_POST['password'] : '';
			$data['local_city'] = isset($_POST['localCity']) ? $_POST['localCity'] : '';
			$data['createtime'] = time();
			$data['updatetime'] = time();
			$data['disabled'] = 0;
			
			//检查验证码是否正确.
			if ($_SESSION['validate_code'] != $_POST['validate']) {
				$this->message('back', '验证码填写有误,请重新填写.', '用户注册');
			}
			
			//检验 昵称是否合法.
			if (!preg_match('/^[a-zA-Z0-9\x{4e00}-\x{9fa5}]{2,10}$/u', $data['nickname'])) {
				$this->message('back', '用户昵称不符合要求,请重新填写.', '用户注册');
			}
			
			//检验用户名是否正确.
			if (!preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/', $data['uname'])) {
				$this->message('back', '用户邮箱不符合要求,请重新填写.', '用户注册');
			}
			
			//检验密码是否正确.
			if (!preg_match('/^[a-zA-Z0-9_]{6,12}$/', $data['password'])) {
				$this->message('back', '用户密码不符合要求,请重新填写.', '用户注册');
			}
			
			$member_model = new MemberModel();
			
			if ($member_model->unameExist($data['uname'])) {
				$this->message('back', '用户名已经存在.', '用户注册');
			}
			
			if ($member_model->nicknameExist($data['nickname'])) {
				$this->message('back', '昵称已经存在.', '用户注册');
			}
			
			$member_model->addMember($data);
		}
		
		$this->display();
	}
	
	/**
	 * 验证码显示 
	 */
	public function validateCode() {
		header('Content-Type: image/png');
		$validateCode = new ValidateCodeModel();
		$_SESSION['validate_code'] = $validateCode->getRandCode();
		$validateCode->createValidateCodeImage($_SESSION['validate_code']);
	}
}